<?php echo view('header.html');?>

<body class="layui-view-body">
    <div class="layui-content">
        <div class="layui-row">
            <div class="layui-card">
                <div class="mokui-card-form">
                    <form class="layui-form" id="searchForm">
                        
                        <div class="layui-form-item">
                            <div class="layui-inline">

                                <div class="layui-input-inline" style="width: 100px;">
                                    <select name="status">
                                        <option value="">全部</option>
                                        <option value="0">不拦截</option>
                                        <option value="1">拦截</option>
                                    </select>
                                </div>

                                <button type="button" class="layui-btn layui-btn-blue" lay-submit lay-filter="ams_search" id="ams_search">查询</button>
                                <button type="button" class="layui-btn layui-btn-primary" lay-submit lay-filter="ams_reset" id="ams_reset">重置</button>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="layui-card-body">
                    <table id="listTable" lay-filter="listTable"></table>
                    <script type="text/html" id="toolbarOption">
                        <button type="button" lay-event="del" title="您确定要这样操作吗？" class="layui-btn layui-btn-danger layui-btn-sm">删除

                        </button>
                        <button class="layui-btn layui-btn-blue layui-btn-sm" lay-submit lay-filter="tool_add">添加IP黑名单</button>
                    </script>

                    <script type="text/html" id="statusTpl">
                        <input type="checkbox" name="status" value="{{d.status}}" lay-skin="switch" lay-text="拦截|不拦截" lay-filter="statusSwitch" {{ d.status == 1 ? 'checked' : '' }}>
                    </script>


                    <script type="text/html" id="barOption">
                        <a class="layui-btn layui-btn-xs" lay-event="edit">修改</a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                    </script>
                </div>
            </div>
        </div>
    </div>
    <?php echo view('footer.html');?>
    <script type="text/javascript">
        layui.use(['form', 'jquery', 'layer', 'table'], function () {
            var $ = layui.jquery,
                form = layui.form,
                layer = layui.layer,
                table = layui.table;

            
            /**index**/

            //数据初始化
            var tableInit = table.render({
                elem: '#listTable',
                toolbar: "#toolbarOption",
                defaultToolbar: [],
                title: 'IP黑名单列表',
                url: adminpath + 'admin/black/index',
                method: 'post',
                cols: [
                    [
                        { type: 'checkbox', width: 50 },
                        { field: 'id', title: 'ID', width: 80, unresize: true, sort: true },
                        { field: 'ip', width: 400, title: '单个IP' },
                        { field: 'status', title: '是否显示', templet: '#statusTpl' },
                        { field: 'endtime_date', title: '截至时间' },
                        { fixed: 'right', title: '操作', toolbar: '#barOption' }
                    ]
                ],
                page: {
                    layout: ['prev', 'page', 'next', 'count'],
                    groups: 5,
                    first: true,
                    last: true
                },
                response: {
                    statusCode: 0,
                    countName: 'count'
                },
                parseData: function (res) {
                    return {
                        'code': res.code,
                        "data": res.data.data,
                        'count': res.data.total
                    };
                }
            });

            //表头工具栏
            table.on('toolbar(listTable)', function (obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                var data = checkStatus.data;
                var ids = [];
                $.each(data, function (i, v) {
                    ids.push(v.id);
                })

                if (ids.length < 1) {
                    layer.msg('您还没有选择', { icon: 5, anim: 6 });
                    return false;
                }

                switch (obj.event) {

                    case 'del':
                        layer.confirm('确定要删除这些？', function (index) {
                            $.post(adminpath + 'admin/black/del', { ids: ids }, function (res) {
                                if (res.code == 0) {
                                    layer.msg('操作成功', { icon: 1, time: 1500 });
                                } else {
                                    layer.msg(res.msg, { icon: 5, anim: 6 });
                                }
                                tableInit.reload();
                            }, 'json');
                            layer.close(index);
                        });
                        break;

                };
            });

            //监听表格行操作
            table.on('tool(listTable)', function (obj) {
                var data = obj.data;
                var id = data.id
                switch (obj.event) {
                    case 'edit':
                        layer.open({
                            type: 2,
                            area: ['600px', '80%'],
                            title: '修改IP黑名单(#' + id + ')',
                            content: [adminpath + 'admin/black/edit?id=' + id]
                        });
                        // var title = '修改IP黑名单(#' + id + ')';
                        // var url = adminpath + 'admin/black/edit?id=' + id;
                        // open_parent_tab(title, url);
                        break;

                    case 'del':
                        layer.confirm('确定要删除吗？', function (index) {
                            $.post(adminpath + 'admin/black/del', { ids: [id] }, function (res) {
                                if (res.code == 0) {
                                    obj.del();
                                    layer.msg(res.msg, { icon: 1, time: 1500 }, function () {
                                        tableInit.reload();
                                    });
                                } else {
                                    layer.msg(res.msg, { icon: 5, anim: 6, time: 1500 }, function () {
                                        tableInit.reload();
                                    });
                                }
                            }, 'json');
                        });
                        break;
                }
            });

            //搜索条件
            form.on('submit(tool_add)', function () {
                var title = '添加IP黑名单';
                var url = adminpath + 'admin/black/add';
                // open_parent_tab(title, url);
                layer.open({
                    type: 2,
                    area: ['700px', '90%'],
                    title: '添加IP黑名单',
                    content: [adminpath + 'admin/black/add']
                });
                return false;
            });


            //监听状态操作
            form.on('switch(statusSwitch)', function (obj) {
                var id = obj.othis.parents('tr').find("td:eq(1)").find(".layui-table-cell").text();
                var title = '';
                var val = '';
                if (this.value == 1) {
                    title = '您确定要[取消拦截]吗?';
                    val = 0;
                } else {
                    title = '您确定要[启用拦截]吗?';
                    val = 1;
                }

                //点确认再切换
                var x = obj.elem.checked;
                obj.elem.checked = !x;
                form.render('checkbox');

                layer.confirm(title, function (index) {
                    $.post(adminpath + "admin/black/edit", { do: 'status', id: id, value: val }, function (res) {
                        if (res.code == 0) {
                            layer.msg('操作成功', { icon: 1, time: 1500 });
                        } else {
                            layer.msg(res.msg, { icon: 5, anim: 6 });
                        }
                        tableInit.reload();
                    }, 'json');
                    layer.close(index);
                });
            });

            //搜索条件
            form.on('submit(ams_reset)', function (data) {
                $("#searchForm")[0].reset();
                form.render();
                $('#ams_search').trigger('click');
            });

            //标题搜索
            form.on('submit(ams_search)', function (data) {
                tableInit.reload({
                    where: data.field
                    , page: {
                        curr: 1
                    }
                });
            });


        })
    </script>
</body>

</html>
